package task

import (
	"fmt"
	"github.com/robfig/cron/v3"
	"log"
	"sync"
)

func TimedStart(ch <-chan int, data string) {
	flow, err := NewFlowTask(nil, data)
	if err != nil {
		fmt.Println("定时任务流程配置不正确！", err)
		return
	}
	node := flow.Data.NodeList[0]
	label := node.Content["cron"]
	log.Printf("定时任务启动:%s", label)
	c := cron.New(cron.WithSeconds())
	c.AddFunc(label, func() {
		flow.wg = sync.WaitGroup{}
		flow.out = make(map[string]map[string]interface{}, 0)
		flow.outList = make(map[string][]map[string]interface{}, 0)
		flow.outText = make(map[string]string, 0)
		flow.outByte = make(map[string][]byte, 0)
		err = flow.Next(node.Id)
		if err != nil {
			log.Println(err)
		}
	})
	c.Start()
	select {
	case <-ch:
		fmt.Println("定时任务监听结果")
	}
	defer c.Stop()
}
